package graphic.mycomponents;

import graphic.ProgramTab;
import graphic.ReleasesPanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import utility.GraphicUtilsFactory;
import core.Client;
import core.ClientParameters;

public class ProgramTable extends JTable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	ProgramTableModel model;
	CellRender render;
	ListSelection selection;
	
	ImageIcon unLocked;
	ImageIcon locked;
	
	public ProgramTable() {
		super();
		
		this.model = new ProgramTableModel();
		this.render = new CellRender();
		this.selection = new ListSelection();
		
		this.unLocked = new ImageIcon(ProgramTab.class.getClassLoader().getResource(ClientParameters.IMAGE_PATH + "unLocked.jpg"));
		this.locked = new ImageIcon(ProgramTab.class.getClassLoader().getResource(ClientParameters.IMAGE_PATH + "locked.jpg"));
		
		super.setModel(this.model);
		super.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
		super.setForeground(Color.black);
		super.setGridColor(Color.black);
		super.setRowHeight(this.locked.getIconHeight());
				
		super.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		super.getSelectionModel().addListSelectionListener(this.selection);
		
		GraphicUtilsFactory.packTableColoumns(this, 2);
		//this.packColumns(2);
	}
	
	@Override
	public TableCellRenderer getCellRenderer(int row, int column) {
		return this.render;
	}
	
	@Override
	public javax.swing.table.DefaultTableModel getModel() {
		return this.model;
	}

	public void packColumns(int margin)
	{ 
		for (int c = 0; c < super.getColumnCount(); c++)
			packColumn(c, 2); 
	}
	
	public void packColumn(int vColIndex, int margin)
	{ 
		DefaultTableColumnModel colModel = (DefaultTableColumnModel)super.getColumnModel(); 
		TableColumn col = colModel.getColumn(vColIndex);
		int width = 0; // Get width of column header 
		TableCellRenderer renderer = col.getHeaderRenderer();
		if (renderer == null) 
			renderer = super.getTableHeader().getDefaultRenderer(); 

		Component comp = renderer.getTableCellRendererComponent(this, col.getHeaderValue(), false, false, 0, 0);
		width = comp.getPreferredSize().width; // Get maximum width of column data 
		for (int r = 0; r < super.getRowCount(); r++) 
		{ 
			renderer = this.getCellRenderer(r, vColIndex); 
			comp = renderer.getTableCellRendererComponent(this, super.getValueAt(r, vColIndex), false, false, r, vColIndex); 
			width = Math.max(width, comp.getPreferredSize().width);
		} // Add margin 
		width += 2*margin; // Set the width 
		col.setPreferredWidth(width);
	}
		
	class CellRender extends DefaultTableCellRenderer
	{
		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus,
				int row, int column) {
			
				JLabel renderedLabel = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

				if(column == 6)
					if((Boolean)ProgramTable.this.getValueAt(row, column))
						return new JLabel(ProgramTable.this.locked, SwingConstants.CENTER);
					else
						return new JLabel(ProgramTable.this.unLocked, SwingConstants.CENTER);
				
				renderedLabel.setHorizontalAlignment(SwingConstants.CENTER);
						
				return renderedLabel;
		}
	}
/*
	@Override
	public Object getValueAt(int row, int column)
	{
		if(column == 6)
			if((Boolean)super.getValueAt(row, column))
				return this.locked;
			else
				return this.unLocked;
		
		return super.getValueAt(row, column);
	}
*/
	public class ProgramTableModel extends DefaultTableModel
	{
		public ProgramTableModel() {
			
			Vector<String> header = new Vector<String>();
			header.add("Id");
			header.add("File Name");
			header.add("Last edit by");
			header.add("Last edit in");
			header.add("Rel");
			header.add("Open By");
			header.add("");
			
			super.setColumnIdentifiers(header);		
		}
		
		@Override
		public boolean isCellEditable(int row, int column) {
			return false;
		}
		
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			return columnIndex == 6 ? ImageIcon.class : Object.class;
		}
		
	}
	
	class ListSelection implements ListSelectionListener
	{	
		@Override
		public void valueChanged(ListSelectionEvent e) {

			ListSelectionModel lsm = (ListSelectionModel)e.getSource();
	        if(!lsm.isSelectionEmpty())
	        {
	            int minIndex = lsm.getMinSelectionIndex();
	            int maxIndex = lsm.getMaxSelectionIndex();
	            for (int i = minIndex; i <= maxIndex; i++)
	                if (lsm.isSelectedIndex(i))
	                {
	                	int fileId = (Integer)ProgramTable.this.getValueAt(i, 0);
	                	Client.getInstance().requestReleaseList(fileId);
	                	ReleasesPanel.getInstance().setFileId(fileId);
	                }
	            	/*
	                if (lsm.isSelectedIndex(i))
	                	ReleasesPanel.getInstance().populateTable((Integer)ProgramTable.this.getValueAt(i, 0));
	                */
	        }
		}
	}
}